package com.onlyphoto.onlymall.base;

import android.accounts.NetworkErrorException;
import android.content.Context;

import com.google.gson.JsonSyntaxException;
import com.onlyphoto.onlymall.BuildConfig;
import com.onlyphoto.onlymall.events.LoginInvalidEvent;
import com.onlyphoto.onlymall.utils.LogUtil;
import com.onlyphoto.onlymall.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import java.lang.Exception;

/**
 * Created by gaochujia on 2019-6-27.
 */

public abstract class BaseObserver<T> implements Observer<BaseNetworkResponse<T>> {
    private Context mContext;

    public BaseObserver(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();
    }

    @Override
    public void onNext(BaseNetworkResponse<T> baseNetworkResponse) {
        if (!baseNetworkResponse.isError()) {
            try {
                onSuccess(baseNetworkResponse.getData(), baseNetworkResponse.getMsg());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                if (baseNetworkResponse.getError_code() == -2) {
                    EventBus.getDefault().post(new LoginInvalidEvent());
                }
                onCodeError(baseNetworkResponse);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        if (null != e.getMessage()) {
            LogUtil.log("error", "onError: " + e.getMessage());
        }
        try {
            if (e instanceof ConnectException
                    || e instanceof TimeoutException
                    || e instanceof NetworkErrorException
                    || e instanceof UnknownHostException) {
                if (null != mContext) {
                    ToastUtil.INSTANCE.showToast("网络异常，请稍后重试");
                }
            }
            if (e instanceof JsonSyntaxException) {
                if (BuildConfig.DEBUG) {
                    ToastUtil.INSTANCE.showToast("Gson解析出错");
                }
            }
            onFailure(e);
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            onFinally();
        }
    }

    @Override
    public void onComplete() {
        onFinally();
    }


    /**
     * 返回成功
     *
     * @param data
     * @throws Exception
     */
    protected abstract void onSuccess(T data, String msg);

    /**
     * 返回成功了,但是code错误
     *
     * @param t
     * @throws Exception
     */
    protected abstract void onCodeError(BaseNetworkResponse<T> t) throws Exception;


    /**
     * 返回失败
     *
     * @param e
     * @throws Exception
     */
    protected abstract void onFailure(Throwable e) throws Exception;

    protected abstract void onRequestStart();

    protected abstract void onFinally();


}
